import { Types, defineComponent, defineQuery, pipe, type IWorld } from '$lib/ecs';

export const Global = defineComponent('Global', {
  gameOver: Types.ui8,
});

export const Clock = defineComponent('Clock', {
  created: Types.ui32,
  time: Types.ui32,
});

export const clockQuery = defineQuery([Clock]);
export const clockOnlyQuery = defineQuery([Clock, Global]);
export const getClock = (world: IWorld) => clockOnlyQuery(world)[0];

const onTick = [];

export const registerOnTick = (system) => {
  onTick.push(system);
};

export const processClock = (world) => {
  return pipe(...onTick)(world);
};

const onTickActive = new Map();

export const getOnTickActive = (component) => onTickActive.get(component);
export const registerOnTickActive = (component, action) => onTickActive.set(component, action);

export const isGameOver = (world: IWorld) => {
  return !!Global.gameOver[getClock(world)];
};
